/*====================================================================================
# Module of «ДЭНСИ: КАССА» complex is the front-office application for POS-systems based on fiscal registrators.
# © 2010, Kostuchenko Alexey (leo-dead@mail.ru).
# This is a FREE software, licensed under GNU/GPL terms. See details in LICENSING_ENG.TXT in main directory
#====================================================================================
# Модуль программного комплекса «ДЭНСИ: КАССА» - рабочее место кассира для POS-систем на базе фискальных регистраторов
# © 2010, Костюченко Алексей (leo-dead@mail.ru).
# Данная программа является свободным программным обеспечением, выпускается на условиях Стандартной Общественной Лицензии GNU/GPL
# Подробнее смотрите в файле LICENSING_RUS.TXT в основной директории
#====================================================================================*/

// TODO при добавлении нового элемента / изменении старого проверять operator_id (не повторяется, > 0)

void cpOperatorsForm::init()
{
    OperatorsTable->setColumnWidth(0, 50);
    OperatorsTable->setColumnWidth(1, 100);
    OperatorsTable->setColumnWidth(2, 200);
    OperatorsTable->setColumnWidth(3, 200);
    OperatorsTable->setColumnWidth(4, 200);
    OperatorsTable->setColumnWidth(5, 50);
    OperatorsTable->setColumnWidth(6, 50);
}


void cpOperatorsForm::destroy()
{

}


int cpOperatorsForm::Load( QString scheme_name )
{
    SetupCyberplat cyberplat_db(LINCASH_CONF_PATH);
    if ( cyberplat_db.er_con != 0 ) {
        return -1;
    }

    QTextCodec* codec_db =  QTextCodec::codecForName(DBCODEC);

    if (cyberplat_db.SetScheme(codec_db->fromUnicode(scheme_name)) != 0) {
        return -1;
    }

    TCyberplatOperator* operators = NULL;
    int count_operators;

    if (cyberplat_db.GetOperators(operators, count_operators) != 0) {
        return -1;
    }

    OperatorsTable->setNumRows(count_operators);

    for ( int i = 0; i < count_operators; i++ )   {
    	OperatorsTable->setText(i, 0, codec_db->toUnicode(operators[i].operator_id));
    	OperatorsTable->setText(i, 1, codec_db->toUnicode(operators[i].operator_name));
    	OperatorsTable->setText(i, 2, codec_db->toUnicode(operators[i].checkurl));
    	OperatorsTable->setText(i, 3, codec_db->toUnicode(operators[i].payurl));
    	OperatorsTable->setText(i, 4, codec_db->toUnicode(operators[i].statusurl));
    	OperatorsTable->setText(i, 5, codec_db->toUnicode(operators[i].account));
    	OperatorsTable->setText(i, 6, codec_db->toUnicode(operators[i].fee));
    }

    free(operators);

    return 0;
}


int cpOperatorsForm::Save( QString scheme_name )
{
    SetupCyberplat cyberplat_db(LINCASH_CONF_PATH);
    if ( cyberplat_db.er_con != 0 ) {
        return -1;
    }

    QTextCodec* codec_db =  QTextCodec::codecForName(DBCODEC);

    if (cyberplat_db.SetScheme(codec_db->fromUnicode(scheme_name)) != 0) {
        return -1;
    }

    if (cyberplat_db.DeleteOperators() != 0) {
        return -1;
    }

    int count_operators = OperatorsTable->numRows();
    TCyberplatOperator op;

    for (int i = 0; i < count_operators; i++ ) {
        strcpy(op.operator_id, codec_db->fromUnicode(OperatorsTable->text(i,0)));
        strcpy(op.operator_name, codec_db->fromUnicode(OperatorsTable->text(i,1)));
        strcpy(op.checkurl, codec_db->fromUnicode(OperatorsTable->text(i,2)));
        strcpy(op.payurl, codec_db->fromUnicode(OperatorsTable->text(i,3)));
        strcpy(op.statusurl, codec_db->fromUnicode(OperatorsTable->text(i,4)));
        strcpy(op.account, codec_db->fromUnicode(OperatorsTable->text(i,5)));
        strcpy(op.fee, codec_db->fromUnicode(OperatorsTable->text(i,6)));
        cyberplat_db.AddOperator(op);
    }

    return 0;
}


void cpOperatorsForm::OperatorsTable_CurrentChanged( int row, int )
{
    if (row < 0) {
        EditButton->setEnabled(false);
        RemoveButton->setEnabled(false);
        return;
    }

    EditButton->setEnabled(true);
    RemoveButton->setEnabled(true);

    OperatorEdit->setText(OperatorsTable->text(row, 0));
    NameEdit->setText(OperatorsTable->text(row, 1));
    CheckUrlEdit->setText(OperatorsTable->text(row, 2));
    PayUrlEdit->setText(OperatorsTable->text(row, 3));
    StatusUrlEdit->setText(OperatorsTable->text(row, 4));
    AccountEdit->setText(OperatorsTable->text(row, 5));
    FeeEdit->setText(OperatorsTable->text(row, 6));
}


void cpOperatorsForm::AddButton_Clicked()
{
    QString operator_id = OperatorEdit->text();
    QString name = NameEdit->text();
    QString checkurl = CheckUrlEdit->text();
    QString payurl = PayUrlEdit->text();
    QString statusurl = StatusUrlEdit->text();
    QString account = AccountEdit->text();
    QString fee = FeeEdit->text();

    int row = OperatorsTable->numRows();
    OperatorsTable->setNumRows(row+1);

    OperatorsTable->setText(row, 0, operator_id);
    OperatorsTable->setText(row, 1, name);
    OperatorsTable->setText(row, 2, checkurl);
    OperatorsTable->setText(row, 3, payurl);
    OperatorsTable->setText(row, 4, statusurl);
    OperatorsTable->setText(row, 5, account);
    OperatorsTable->setText(row, 6, fee);

    OperatorsTable->setCurrentCell(row, 0);

    emit DataChanged();
}


void cpOperatorsForm::EditButton_Clicked()
{
    int row = OperatorsTable->currentRow();
    if (row == -1) return;

    OperatorsTable->setText(row, 0, OperatorEdit->text());
    OperatorsTable->setText(row, 1, NameEdit->text());
    OperatorsTable->setText(row, 2, CheckUrlEdit->text());
    OperatorsTable->setText(row, 3, PayUrlEdit->text());
    OperatorsTable->setText(row, 4, StatusUrlEdit->text());
    OperatorsTable->setText(row, 5, AccountEdit->text());
    OperatorsTable->setText(row, 6, FeeEdit->text());

    emit DataChanged();
}


void cpOperatorsForm::RemoveButton_Clicked()
{
    int row = OperatorsTable->currentRow();
    if (row == -1) return;
    OperatorsTable->removeRow(row);
    if (row>OperatorsTable->numRows()) row--;
    OperatorsTable->setCurrentCell(row, 0);

    emit DataChanged();
}



